/*
 * LimitaEdits.java
 * 
 */

package biermanager.editorDeTimes.uteis;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

/**
 *
 * @author Tiago Silva Loureiro
 */
    
 public class LimitaEdits extends PlainDocument
 {
     private int tamanhoMax;
     
 public LimitaEdits(int max) {
         super();
         tamanhoMax = max;
 }
    @Override
 public void insertString(int offset, String str, AttributeSet a)throws BadLocationException{
         if ( str == null ) return; 
         if ( tamanhoMax < 1 )
         {
             super.insertString(offset, str, a);
             return;
         }
         int tamanhoAtual = ( getLength() + str.length() );
         if ( tamanhoAtual <= tamanhoMax )
             super.insertString(offset, str, a);
         
        else
         {
             if ( getLength() == tamanhoMax ) return;
             String str2 = str.substring(0, ( tamanhoMax - getLength()));
             
             super.insertString(offset, str2, a);
        }
     }
}
